//
//  YJWTabBar.m
//  百思不得骑姐
//
//  Created by YJW on 16/10/10.
//  Copyright © 2016年 yijiang. All rights reserved.
//

#import "YJWTabBar.h"

@interface YJWTabBar ()

/** publish 按钮   */
@property(nonatomic,strong) UIButton *publishBtn;

@end

@implementation YJWTabBar

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self == [super initWithFrame:frame]) {
        
        //设置 tabBar 的背景图片
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
        
        UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        
        [self addSubview:publishBtn];
        
        self.publishBtn = publishBtn;
    }
    
    return self;
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat width = self.width;
    CGFloat height = self.height;
    
    //设置 publishBtn 的 frame
    self.publishBtn.size = self.publishBtn.currentBackgroundImage.size;
    self.publishBtn.center = CGPointMake(width * 0.5, height * 0.5);
    
    //设置其它按钮的 frame
    NSInteger index = 0;
    CGFloat btnW = width / 5;
    CGFloat btnH = height;
    
    for (UIView *button in self.subviews) {
        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        
        CGFloat x = btnW * ((index>1)?(index+1):index);
        
        button.frame = CGRectMake(x, 0, btnW, btnH);
        
        index++;
    }
}

@end
